#!/usr/bin/perl

#	                 PCtoMobile用 index.cgi VER 1.00

#================================================================
#このスクリプトはフリーソフトです。このスクリプトを使用したいかな
#る損害に対しても作者は一切の責任を負いません。
#===========================使用法===============================
# 次のようにして呼び出してください。
#		http://・・・・・/
#		http://・・・・・/index.cgi
#
# cgi-lib.plへのパス
require './cgi-lib.pl';

&ReadParse;

while (($key,$val) = each %in) {
	$in{$key} = $val;
}
#================================================================
# ▼設定▼ジャンプ先ＵＲＬ(フルパス)の設定
#================================================================
$keitai ="./k_shop.cgi?a8=$in{'a8'}";		#携帯用

#================================================================
# ▼設定終了▼
#================================================================
#================================================================
# 機種判別
#================================================================

if($ENV{'HTTP_USER_AGENT'} =~ /^(docomo\/1)/i){	# i-mode
	$browser   = "i";
	$carrier ="i-mode";

}elsif($ENV{'HTTP_USER_AGENT'} =~ /^(L-mode)/i){# L-mode
	$browser   = "i";
	$carrier ="i-mode";

}elsif($ENV{'HTTP_USER_AGENT'} =~ /^(ASTEL)/i){	# ASTEL
	$browser   = "i";
	$carrier ="ASTEL";

}elsif($ENV{'HTTP_USER_AGENT'} =~ /^j\-phone/i){# J-PHONE
	$browser   = "j";
	$carrier ="j-sky";
	
}elsif($ENV{'HTTP_USER_AGENT'} =~ /Vodafone/i){# J-PHONE
	$browser   = "j";
	$carrier ="j-sky";

}elsif($ENV{'HTTP_USER_AGENT'} =~ /^SoftBank/i){		# J-PHONE
        $browser   = "j";
        $carrier ="j-sky";

}elsif($ENV{'HTTP_USER_AGENT'} =~ /^(docomo\/2)/i){# XHTML(foma)
	$browser   = "f";
	$carrier ="i-mode";

}elsif($ENV{'HTTP_USER_AGENT'} =~ /^(KDDI)/i){# XHTML(ezweb)
	$browser   = "x";
	$carrier ="ezweb";

}elsif($ENV{'HTTP_USER_AGENT'} =~ /^up\.browser/i){# HDML >>>XHTML
	$browser   = "x";
	$carrier ="ezweb";

}else{										# Other(PC)
	$browser   ="pc";
	$carrier ="other";

}

#================================================================
# 機種判別情報に基づいてジャンプ
#================================================================

if ( $browser eq 'pc' ) {
	&top_html();
} else {
	print "Location: $keitai\n\n";
}

exit;
#===============トップページの生成=======================================================
sub top_html{

#==================キーワードの半角ブランクを全角に変換============================
#$in{'keyword'} =~ s/\ /\　/g;

$keyword_encode =$in{'keyword'};
$keyword_encode =~ s/(\W)/sprintf("%%%02X", unpack("C", $1))/eg;    #ここでエンコード

$order_encode =$in{'order'};
$order_encode =~ s/(\W)/sprintf("%%%02X", unpack("C", $1))/eg;    #ここでエンコード
$order_encode2 =$order_encode;

$class_encode =$in{'class'};
$class_encode =~ s/(\W)/sprintf("%%%02X", unpack("C", $1))/eg;    #ここでエンコード

# 設定ファイル====================================================================
require './set.pl';

$url_goods_no ="$script?id";

$script_save ="$script?order=$order_encode&amp;class=$class_encode&amp;keyword=$keyword_encode&amp;FF=$in{'FF'}&amp;pic_only=$in{'pic_only'}";
$into_cart_url =$script_save."&amp;mode=look";
$url_class ="$script?superkey=1&amp;FF=0&amp;pic_only=$in{'pic_only'}&amp;class";
$url_keyword ="$script?FF=0&amp;pic_only=$in{'pic_only'}&amp;keyword";
$url_mode ="$script?t";
$url_mode =~ s/shop.cgi/index.cgi/g;

$kensaku_form = <<"EOF";
<form method='post' action="$script" style='margin: 0px;'>
<input type="text" size='20' name="keyword" value="$pas_word" class='kensaku'$xslash>
<input type="hidden" name="class" value="all"$xslash>
<input type='submit' value="商品検索" class='submit'$xslash>
<input type="hidden" name="FF" value='0'$xslash>
<input type="hidden" name="superkey" value="1"$xslash>
<input type="hidden" name="order" value="$in{'order'}"$xslash>
<input type="hidden" name="price_sort" value="$in{'price_sort'}"$xslash>
<input type="hidden" name="pic_only" value="$in{'pic_only'}"$xslash>
</form>
EOF

$into_cart_form = <<"EOF";
<form method="post" action="$script" style='margin: 0px;'>
<input type="hidden" name="order" value="$in{'order'}"$xslash>
<input type="hidden" name="class" value="$in{'class'}"$xslash>
<input type="hidden" name="keyword" value="$in{'keyword'}"$xslash>
<input type="hidden" name="superkey" value="$in{'superkey'}"$xslash>
<input type="hidden" name="price_sort" value="$in{'price_sort'}"$xslash>
<input type="hidden" name="pic_only" value="$in{'pic_only'}"$xslash>
<input type="hidden" name="mode" value="look"$xslash>
<input type='submit' value="レジへ進む" class='submit'$xslash>
</form>
EOF

if($in{'t'} eq '1'){
	$top_html =$top1_html;
	$top_name =$top1_name;
}elsif($in{'t'} eq '2'){
	$top_html =$top2_html;
	$top_name =$top2_name;
}elsif($in{'t'} eq '3'){
	$top_html =$top3_html;
	$top_name =$top3_name;
}elsif($in{'t'} eq '4'){
	$top_html =$top4_html;
	$top_name =$top4_name;
}elsif($in{'t'} eq '5'){
	$top_html =$top5_html;
	$top_name =$top5_name;
}else{
	$top_html =$top1_html;
	$top_name ='';
}

$log_in_id =$hello_id;
$log_in_name =$hello_id;
if($id_pass_C ne '' || $id_pass2_C ne ''){
	$log_in_name =$kanji_c;
	$log_in_id =$id_name_C;
}

$top_html =~ s/\$log_in_id/$log_in_id/g;
$top_html =~ s/\$log_in_name/$log_in_name/g;
$top_html =~ s/\$log_in_mess/$log_in_mess/g;

$top_html =~ s/\$url_goods_no/$url_goods_no/g;
$top_html =~ s/\$url_class/$url_class/g;
$top_html =~ s/\$url_keyword/$url_keyword/g;
$top_html =~ s/\$kensaku_form/$kensaku_form/g;
$top_html =~ s/\$into_cart_url/$into_cart_url/g;
$top_html =~ s/\$into_cart_form/$into_cart_form/g;
$top_html =~ s/\$url_top/$url_mode/g;
$top_html =~ s/\$url_mode/$url_mode/g;

$top_html =~ s/\$script/$script/g;
$top_html =~ s/\$order_encode/$order_encode/g;
$top_html =~ s/\$class_encode/$class_encode/g;
$top_html =~ s/\$keyword_encode/$keyword_encode/g;
$top_html =~ s/\$in{'pic_only'}/$in{'pic_only'}/g;
$top_html =~ s/\$in{'FF'}/$in{'FF'}/g;
$top_html =~ s/\$in{'price_sort'}/$in{'price_sort'}/g;
$top_html =~ s/\$pic_only/$in{'pic_only'}/g;
$top_html =~ s/\$page_no/$in{'FF'}/g;
$top_html =~ s/\$price_sort/$in{'price_sort'}/g;

&html_header();

print <<"EOF";
$top_html
EOF
	print "$foot</body></html>" ;	

}
#======================================================================
# Cookieに値を書き込むためのSet-Cookie:ヘッダを生成する
#
sub setCookie{
    local($tmp, $val);
    $val = $_[1];
    $val =~ s/(\W)/sprintf("%%%02X", unpack("C", $1))/eg;
    $tmp = "Set-Cookie: ";
    $tmp .= "$_[0]=$val; ";
    $tmp .= "expires=Thu, 1-Jan-2050 00:00:00 GMT;\n";
    return($tmp);
}
sub setCookie_0{
    local($tmp, $val);
    $val = $_[1];
    $val =~ s/(\W)/sprintf("%%%02X", unpack("C", $1))/eg;
    $tmp = "Set-Cookie: ";
    $tmp .= "$_[0]=$val; ";
    $tmp .= ";\n";
    return($tmp);
}
#
# Cookieの値を読み出す
#
sub getCookie{
    local($xx, $name, $value);
    for $xx (split(/; */, $ENV{'HTTP_COOKIE'})) {
        ($name, $value) = split(/=/, $xx);
        $value =~ s/%([0-9A-Fa-f][0-9A-Fa-f])/pack("C", hex($1))/eg;
        $COOKIE{$name} = $value;
    }
}

sub html_header{
	if($top_name){
		$top_key =$top_name.',';
		$top_descrip ="$top_name";
		$top_name .="/";
	}

	print "Content-type: text/html\n\n";

if($xhtml_use){
	if($xhtml_use ne 2){print "<?xml version='1.0' encoding='Shift_JIS'?>\n";}
	print "<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Transitional//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'>\n";
	print "<html xmlns='http://www.w3.org/1999/xhtml' xml:lang='ja' lang='ja'>\n";
}else{
	print "<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">\n";
	print "<html lang='ja'>\n";
}
	print "<head>\n";
	print "<meta content=\"text/html; charset=Shift_JIS\" http-equiv='Content-Type'$xslash>\n";
	print "<title>$top_name$main_title</title>\n";
	print "<meta name=\"keywords\" content=\"$top_key$shop_keywords\"$xslash>\n";
	print "<meta name=\"description\" content=\"$shop_description$top_descrip\"$xslash>\n";
	print "<meta http-equiv=\"Content-Script-Type\" content=\"text/javascript\"$xslash>\n";
	print "<meta content=\"text/css\" http-equiv='Content-Style-Type'$xslash>\n";
if($in{'t'} eq ''){print "<link rel=\"alternate\" type=\"application/rss+xml\" title=\"ROR\" href=\"ror.xml\"$xslash>\n";}
	print "$style";
	
	print "</head>$body$header\n";

}

